<template>
  <div id="app">
    <div class="header">
      <div class="top">
        <div class="left">
          <h3><nut-icon @click="back" name="left"></nut-icon></h3>
        </div>
        <div class="right">
          <h3>{{ city }}-{{ citys }}</h3>
        </div>
      </div>
      <div class="button">
        <div class="left" style="color: green">前一天</div>
        <div class="count">{{ newTime }}</div>
        <div class="right" style="color: green">后一天</div>
      </div>
    </div>
    <div class="zw" style="height: 82px"></div>
    <div class="main">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <dl v-for="(v, i) in datas" :key="i" @click="moment(v)">
            <ul>
              <li>{{ v.dTime }}</li>
              <li>{{ v.dStation }}</li>
              <li>{{ v.trainNumber }}</li>
              <li style="color: orange">{{ v.priceMsg }}</li>
            </ul>
            <ul>
              <li>{{ v.aTime }}</li>
              <li>{{ v.aStation }}</li>
              <li>{{ v.time }}</li>
              <li style="color: orange">{{ v.trainShowDesc }}</li>
            </ul>
          </dl>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="zw"></div>
    <div class="footer">
      <nut-tabbar unactive-color="#7d7e80" active-color="#3cc">
        <nut-tabbar-item
          v-show="departFast"
          tab-title="出发 早->晚"
          icon="home"
          @click="depart"
        ></nut-tabbar-item>
        <nut-tabbar-item
          v-show="departShort"
          tab-title="出发 短->长"
          icon="category"
          @click="depart"
        ></nut-tabbar-item>
        <nut-tabbar-item
          tab-title="只看高铁动车"
          icon="jdl"
          @click="highSpeedbtn()"
        ></nut-tabbar-item>
        <nut-tabbar-item
          tab-title="只看有票"
          icon="scan2"
          @click="haveTicketbtn()"
        ></nut-tabbar-item>
      </nut-tabbar>
    </div>
  </div>
</template>

<script lang="ts">
import { api } from "../../api/api";
import { reactive, ref, toRefs, onBeforeMount, onMounted } from "vue";
import router from "../../router";
import BScroll from "@better-scroll/core";
import Pullup from "@better-scroll/pull-up";
import _ from "lodash";
BScroll.use(Pullup);
export default {
  setup() {
    const state = reactive({
      departFast: true,
      departShort: false,
      newTime: "",
      haveTicket: false,
      highSpeed: false,
      timeSort: 0,
      city: JSON.parse(localStorage.getItem("city") || ""),
      citys: JSON.parse(localStorage.getItem("citys") || ""),
    });
    const list = ref([]);
    const loading = ref(false);
    const finished = ref(false);
    const refreshing = ref(false);
    onBeforeMount: {
      let weeks = ["日", "一", "二", "三", "四", "五", "六"];
      state.newTime =
        new Date().getMonth() +
        "月" +
        new Date().getDate() +
        "日 " +
        "周" +
        weeks[new Date().getDay()];
    }
    const onLoad = _.throttle(() => {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        if (refreshing.value) {
          datas.length = 0;

          refreshing.value = false;
        }
        datas.push(...ledatas);
        // 加载状态结束
        loading.value = false;

        // 数据全部加载完成
        if (datas.length >= 40) {
          finished.value = true;
        }
      }, 1500);
    });
    const onRefresh = () => {
      // 清空列表数据
      finished.value = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      loading.value = true;
      console.log(refreshing.value);

      onLoad();
    };
    // 返回上一页
    function back() {
      history.go(-1);
    }
    // 点击进去选票
    function moment(val: any) {
      console.log(val);
      localStorage.setItem("moment", JSON.stringify(val));

      router.push("/moment");
    }
    // 切换
    function depart() {
      if (state.timeSort == 0) {
        state.timeSort = 1;
      } else {
        state.timeSort = 0;
      }
      state.departFast = !state.departFast;
      state.departShort = !state.departShort;
      api.query
        .gener(state.haveTicket, state.highSpeed, state.timeSort)
        .then((res: any) => {
          Object.assign(datas, res.data.dataMap.directTrainInfo.trains);
        });
    }
    //只看高铁动车切换
    function highSpeedbtn() {
      state.highSpeed = !state.highSpeed;
      api.query
        .gener(state.haveTicket, state.highSpeed, state.timeSort)
        .then((res: any) => {
          Object.assign(datas, res.data.dataMap.directTrainInfo.trains);
        });
    }
    //只看有票切换
    function haveTicketbtn() {
      state.haveTicket = !state.haveTicket;
      api.query
        .gener(state.haveTicket, state.highSpeed, state.timeSort)
        .then((res: any) => {
          Object.assign(datas, res.data.dataMap.directTrainInfo.trains);
        });
    }
    const datas = reactive([]);
    const ledatas = reactive([]);
    console.log(datas);
    api.query
      .gener(state.haveTicket, state.highSpeed, state.timeSort)
      .then((res: any) => {
        Object.assign(datas, res.data.dataMap.directTrainInfo.trains);
        Object.assign(ledatas, res.data.dataMap.directTrainInfo.trains);
      });

    return {
      ...toRefs(state),
      // 返回上一页
      back,
      datas,
      // 订票页面
      moment,
      // 切换
      depart,
      highSpeedbtn,
      haveTicketbtn,
      list,
      onLoad,
      loading,
      finished,
      refreshing,
      onRefresh,
      ledatas,
    };
  },
};
</script>

<style lang="scss" scoped>
#app {
  width: 100%;
  height: 100%;
  background: #eee;
}
html,
body {
  width: 100%;
  background: #eee;
}
.header {
  width: 100%;
  background: #1ba9ba;
  height: 2rem;
  position: fixed;
  top: 0;
  z-index: 999;
  .button {
    height: 1rem;
    background: #eee;
    line-height: 1rem;
    display: flex;
    .count {
      flex: 1;
      text-align: center;
      color: green;
    }
    .left {
      margin-left: 5%;
    }
    .right {
      margin-right: 5%;
    }
  }
}
.top {
  display: flex;
  color: white;
  height: 1.2rem;
  line-height: 1.2rem;
  .left {
    margin-left: 2%;
  }
  .right {
    flex: 1;
    text-align: center;
  }
}
.bottom {
  width: 84%;
  margin-left: 8%;
  height: 1.3rem;
  line-height: 1.3rem;
}

.main {
  width: 94%;
  margin-left: 3%;
  // height: 100%;
  dl {
    width: 100%;
    height: 1.8rem;
    border-bottom: 1px solid #ccc;
    background: white;
    z-index: 1;
    ul {
      display: flex;
      li {
        width: 25%;
        height: 0.9rem;
        line-height: 0.9rem;
        text-align: center;
        font-size: 0.36rem;
      }
    }
  }
}
.zw {
  height: 1rem;
}
.footer {
  width: 100%;
  position: fixed;
  z-index: 100;
  bottom: 0;
  height: 1.3rem;
}
</style>
